package com.yc.io.excel;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;

public class TestExcel {

	public void m0() {
		try {
			ExcelReaderUtil excel = new ExcelReaderUtil();
			excel.readOneSheet("D:/source/word/test1117.xlsx", 1);
			System.out.println(excel.getDataList());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void m1() {
		try {
			String res = ExcelUtils.readExcel("D:/source/word/test1117.xlsx", 1);
			System.out.println(res);
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public void m2() {

	}

	/**
	 * 读取出filePath中的所有数据信息
	 *
	 * @param filePath excel文件的绝对路径
	 */
	public static void getDataFromExcel(String filePath) {
		//String filePath = "E:\\123.xlsx";
		//判断是否为excel类型文件
		if (!filePath.endsWith(".xls") && !filePath.endsWith(".xlsx")) {
			System.out.println("文件不是excel类型");
		}
		FileInputStream fis = null;
		Workbook wookbook = null;
		try {
			//获取一个绝对地址的流
			fis = new FileInputStream(filePath);
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			//2003版本的excel，用.xls结尾
			wookbook = new HSSFWorkbook(fis);//得到工作簿
		} catch (Exception ex) {
			//ex.printStackTrace();
			try {
				//2007版本的excel，用.xlsx结尾
				wookbook = new XSSFWorkbook(fis);//得到工作簿
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//得到一个工作表
		Sheet sheet = wookbook.getSheetAt(0);
		//获得表头
		Row rowHead = sheet.getRow(0);
		//判断表头是否正确
		if (rowHead.getPhysicalNumberOfCells() != 3) {
			System.out.println("表头的数量不对!");
		}
		//获得数据的总行数
		int totalRowNum = sheet.getLastRowNum();
		//要获得属性
		String name = "";
		int latitude = 0;
		//获得所有数据
		for (int i = 1; i <= totalRowNum; i++) {
			//获得第i行对象
			Row row = sheet.getRow(i);
			//获得获得第i行第0列的 String类型对象
			Cell cell = row.getCell((short) 0);
			name = cell.getStringCellValue().toString();
			//获得一个数字类型的数据
			cell = row.getCell((short) 1);
			latitude = (int) cell.getNumericCellValue();
			System.out.println("名字：" + name + ",经纬度：" + latitude);
		}
	}


	public void convertText2Excel(String filepath) {
		BufferedReader bufferedReader = null ;
		FileReader fileReader = null;
		try {
			fileReader = new FileReader(filepath);
			bufferedReader = new BufferedReader(fileReader);
			String str = "";
			while ((str = bufferedReader.readLine()) != null) {

				System.out.println(str);
				System.out.println("-----------------" + str.length());
			}

		} catch (FileNotFoundException e) {
			System.err.println("获取连接失败!!!");
		} catch (IOException e) {
			System.err.println("读取文本内容失败");
		}finally {
			try {
				if(bufferedReader != null)
					bufferedReader.close();
			} catch (IOException e) {
				System.err.println("流关闭失败");
			}
		}


	}





	public static void main(String[] args) {
		TestExcel te = new TestExcel();
//		te.m0();
//		te.m1();
		te.convertText2Excel("C:\\Users\\yundu\\Downloads\\1638730803417.xlsx");
	}
}